package com.heima.user.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.model.common.dtos.ApUserDto;
import com.heima.user.model.common.dtos.LoginDto;
import com.heima.user.model.common.dtos.ResponseResult;
import com.heima.user.model.common.enums.AppHttpCodeEnum;
import com.heima.user.model.common.pojos.ApUser;
import com.heima.user.service.IApUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.utils.common.AppJwtUtil;
import com.heima.user.utils.common.SaltUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-25
 */
@Service
@Slf4j
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements IApUserService {

    @Override
    public ResponseResult loginAuth(LoginDto dto) {
         JSONObject jsonObject = new JSONObject();
        if (StringUtils.hasText(dto.getPhone()) && StringUtils.hasText(dto.getPassword())){
            ApUser apUser = this.lambdaQuery().eq(ApUser::getPhone, dto.getPhone()).one();
            if (apUser == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.USER_NOT_EXIST_OR_PASSWORD_ERROR);
            }
            String salt = apUser.getSalt();
            String password = dto.getPassword();
            String md5Hex = DigestUtils.md5Hex((password + salt).getBytes());

            if (!md5Hex.equals(apUser.getPassword())){
                return ResponseResult.errorResult(AppHttpCodeEnum.USER_NOT_EXIST_OR_PASSWORD_ERROR);
            }

            apUser.setPassword("");
            apUser.setSalt("");
            jsonObject.put("token", AppJwtUtil.getToken(Long.valueOf(apUser.getId())));
            jsonObject.put("user",apUser);
        }else {
            jsonObject.put("token",AppJwtUtil.getToken(0L));
        }

        return ResponseResult.okResult(jsonObject);
    }

    @Override
    public ResponseResult register(ApUserDto dto) {
        //手机号注册
    if (!StringUtils.hasText(dto.getPhone()) || !StringUtils.hasText(dto.getPassword())){
       return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }
        ApUser apUser = this.lambdaQuery().eq(ApUser::getPhone, dto.getPhone()).one();
        String password = dto.getPassword();
        String phone = dto.getPhone();
        if (apUser != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_PHONE_EXIST);
        }
        String salt = SaltUtils.getSalt(3);
        ApUser user = new ApUser();
        user.setPassword(DigestUtils.md5Hex((password + salt).getBytes()));

        //添加到数据库
        user.setPhone(phone);
        user.setCreatedTime(LocalDateTime.now());
        user.setName(dto.getName());
        user.setSalt(salt);
        user.setStatus(1);
        user.setFlag(1);
        this.save(user);
        log.info("用户注册成功");

        return ResponseResult.okResult("用户注册成功");

    }
}
